home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interactive Web Graphics with Shout 3D
/
Interactive Web Graphics With Shout 3D.iso
/
pc
/
Code
/
Chapter Code.exe
/
Chapter08
/
RotateMoveZoomPanel.java
< prev
next >
Wrap
Text File
|
2000-07-03
|
3KB
|
127 lines
package applets;
import shout3d.*;
import shout3d.core.*;
import shout3d.math.*;
public class RotateMoveZoomPanel extends Shout3DPanel implements DeviceObserver{
Viewpoint camera;
float fov;
int pixelStartX;
int pixelStartY;
int pixelEndX;
int pixelEndY;
float[] worldPos = new float[3];
float [] eulers = new float [3];
float [] axisAngle = new float [4];
Quaternion q = new Quaternion();
public RotateMoveZoomPanel (Shout3DApplet applet){
super(applet);
}
public void customInitialize() {
addDeviceObserver(this,"MouseInput", null);
camera = (Viewpoint) getCurrentBindableNode("Viewpoint");
worldPos = camera.position.getValue();
axisAngle = camera.orientation.getValue();
q.setAxisAngle(axisAngle);
q.getEulers(eulers);
fov = camera.fieldOfView.getValue();
}
protected void finalize() {
removeDeviceObserver(this,"MouseInput");
}
public boolean onDeviceInput(DeviceInput di, Object userData) {
MouseInput mi = (MouseInput) di;
switch (mi.which){
case MouseInput.DOWN:
pixelStartX = mi.x;
pixelStartY = mi.y;
return true;
case MouseInput.DRAG:
//if left button used
if (mi.button == 0) {
pixelEndX = mi.x;
pixelEndY = mi.y;
int dragDistanceX = pixelEndX - pixelStartX;
int dragDistanceY = pixelEndY - pixelStartY;
pixelStartX = pixelEndX;
pixelStartY = pixelEndY;
//ROTATION
float rotationDelta = -(dragDistanceX/30f);
eulers[0] = eulers[0] + rotationDelta;
q.setEulers(eulers);
q.getAxisAngle(axisAngle);
camera.orientation.setValue(axisAngle);
//TRANSLATION
float translationDelta = dragDistanceY * 2.0f;
float[] vector = {0,0, translationDelta};
q.xform(vector);
worldPos[0] = worldPos[0] + vector[0];
worldPos[2] = worldPos[2] + vector[2];
camera.position.setValue(worldPos);
return true;
}//end of 0 if
//if right button used
if (mi.button == 1) {
pixelEndY = mi.y;
int dragDistanceY = pixelEndY - pixelStartY;
//adjust field of view at
//150 pixels dragged per
//radian (57.2 degrees)
float fovDelta = dragDistanceY/150.0f;
fov = fov + fovDelta;
camera.fieldOfView.setValue(fov);
pixelStartY = pixelEndY;
return true;
}//end of 1 if
}//end of switch
return false;
}
} //end of class